<template>
  <div>
    <el-button type="primary" @click="addTrademark">添加品牌</el-button>
    <el-table style="margin-top: 20px" border :data="list">
      <el-table-column type="index" label="序号" align="center" width="80px"></el-table-column>
      <el-table-column label="品牌名称" align="center" prop="tmName"></el-table-column>
      <el-table-column label="品牌LOGO" align="center">
        <template slot-scope="{ row }">
          <img :src="row.logoUrl" alt="" style="width: 100px; height: 100px" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" prop="prop">
        <template slot-scope="{ row }">
          <el-button type="warning" icon="el-icon-edit" size="mini" @click="editrow(row)">修改</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteTradeMark(row)">删除</el-button>

        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加品牌" :visible.sync="dialogFormVisible">
      <el-form>
        <el-form-item label="品牌名称" v-model="tmForm" prop="tmName" ref="yzform">
          <el-input utocomplete="off" style="width:60%;margin-left: 14px;" v-model="tmForm.tmName"></el-input>
        </el-form-item>
        <el-form-item label="品牌LOGO" prop="logoUrl">
          <el-upload class="avatar-uploader" action="/dev-api/admin/product/fileUpload" :show-file-list="false"
            :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
            <img v-if="tmForm.logoUrl" :src="tmForm.logoUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div slot="tip" class="el-upload__tip" style="margin-left:90px">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="addOrUpdateTrademark">确 定</el-button>
      </div>
    </el-dialog>

    <el-pagination style="margin-top: 20px; text-align: center" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" :current-page="page" :page-sizes="[3, 5, 10]" :page-size="limit"
      layout=" prev, pager, next, jumper, ->, sizes,total" :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: "TradeMark",
  data() {
    return {
      page: 1,
      limit: 5,
      total: 0,
      list: [],
      dialogFormVisible: false,
      imageUrl: '',
      tmForm: {
        tmName: '',
        logoUrl: '',
        id: ''
      },

    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList(pager = 1) {
      let { page, limit } = this;
      let result = await this.$API.reqTradeMarkList(page, limit);
      if (result.code == 200) {
        this.total = result.data.total;
        this.list = result.data.records;
      }
    },
    handleCurrentChange(pager) {
      this.page = pager;
      this.getList();
    },
    handleSizeChange(limit) {
      this.limit = limit;
      this.getList();
    },
    editrow(row) {
      this.dialogFormVisible = true;
      this.tmForm.logoUrl = row.logoUrl
      this.tmForm.tmName = row.tmName
      this.tmForm.id = row.id
    },
    addTrademark() {
      this.dialogFormVisible = true;
    },
    handleAvatarSuccess(res, file) {
      this.tmForm.logoUrl = res.data
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    cancle() {
      this.$message({
        type: 'info',
        message: '取消成功'
      })
      this.dialogFormVisible = false;
      this.tmForm = { tmName: '', logoUrl: '' }
    },
    async addOrUpdateTrademark() {
      if (this.tmForm.tmName && this.tmForm.logoUrl) {
        let result = await this.$API.reqAddOrUpdateTradeMark(this.tmForm)
        if (result.code == 200) {
          this.$message.success(this.tmForm.id ? '修改品牌成功' : '添加品牌成功'),
            this.dialogFormVisible = false;
          this.tmForm = {}
          if (this.tmForm.id) {
            this.page = page
            this.getList();
          } else {
            this.page = 1
            this.getList();
          }
        }
      } else {
        this.$message.error('请补齐品牌的内容信息')
      }
    },
    deleteTradeMark(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let result = await this.$API.reqDeleteTradeMark(row.id)
        if (result.code == 200) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getList();
        }


      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>


 let id =row.id;
      console.log(id);
      let result = await this.$API.reqDeleteTradeMark(id)
      console.log(result);



      